package cn.wolfcode.web.controller;


import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.PayVo;
import cn.wolfcode.feign.PayFeignApi;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;

    @RequestMapping("/alipay")
    public Result<String> alipay(String orderNo, int type) {
        //校验参数
        if (StringUtils.isEmpty(orderNo) && StringUtils.isEmpty(type + "")) {
            throw new BusinessException(SeckillCodeMsg.SECKILL_PARAM_ERROR);
        }
        //判断是本地支付还是积分支付
        if (OrderInfo.PAYTYPE_ONLINE.equals(type)) {
            //执行付款流程
            String result = orderInfoService.payOnline(orderNo);

            return Result.success(result);
        } else if (OrderInfo.PAYTYPE_INTERGRAL.equals(type)) {
            //执行积分支付流程
         orderInfoService.payIntergral(orderNo);
        } else {
            throw new BusinessException(SeckillCodeMsg.PAY_SERVER_ERROR);
        }
        return Result.success("");
    }


    @Value("${pay.frontEndPayUrl}")
    private String frontEndPayUrl;
    @Autowired
    private PayFeignApi payFeignApi;

    @RequestMapping("/return_url")
    public void returnUrl(@RequestParam Map<String, String> params, HttpServletResponse response) throws IOException {
        //同步回调
        System.out.println("同步回调时间" + new Date());
        //跳转到订单详情界面
        //调用远程支付进行验签
        Result<Boolean> result = payFeignApi.checkSign(params);  //调用SDK验证签名
        String orderNo = params.get("out_trade_no");
        if (result != null && !result.hasError()) {
            if (result.getData()) {
                response.sendRedirect(frontEndPayUrl + orderNo);
            } else {
                response.sendRedirect("http://localhost/50x.html");
            }
        }
    }

    @RequestMapping("/notify_url")
    public void notifyUrl(@RequestParam Map<String, String> params, HttpServletResponse response) throws IOException {
        //异步回调
        System.out.println("异步回调时间" + new Date());
        //进行验签
        Result<Boolean> result = payFeignApi.checkSign(params);//调用SDK验证签名
        PrintWriter out = response.getWriter();
        String orderNo = params.get("out_trade_no");
        if (result != null && !result.hasError()) {
            if (result.getData()) {
                //插入一条流水号
                orderInfoService.paySuccess(orderNo);
                //修改订单状态
                out.println("success");
            } else {
                out.println("fail");
            }
        }
    }

    @RequestMapping("/refund")
    public Result refund(String orderNo) {
        //校验参数
        if (StringUtils.isEmpty(orderNo)) {
            throw new BusinessException(SeckillCodeMsg.SECKILL_PARAM_ERROR);
        }
        //判断是支付宝退款还是积分退款
        OrderInfo orderInfo = orderInfoService.find(orderNo);

        if (OrderInfo.PAYTYPE_ONLINE.equals(orderInfo.getPayType())) {
            //执行支付宝退款
            orderInfoService.refundOnline(orderInfo);
            return Result.success("");
        } else if (OrderInfo.PAYTYPE_INTERGRAL.equals(orderInfo.getPayType())) {
            //执行积分退款
            orderInfoService.refundIntergral(orderInfo);
        } else {
            throw new BusinessException(SeckillCodeMsg.REFUND_ERROR);
        }
        return Result.success("");
    }
}
